package com.hst.capacity.infra.persistence.jpa.order.specialcar;

import com.hst.capacity.domain.model.entity.order.specialcar.SpCarOrderEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface SpCarOrderRepository extends JpaRepository<SpCarOrderEntity, String>, JpaSpecificationExecutor<SpCarOrderEntity> {
    /**
     * 更新订单司机
     * @param idDriver
     * @param idOrder
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("update SpCarOrderEntity t set t.idDriver = ?1 where t.idOrder = ?2")
    int updateDriver(String idDriver,String idOrder);

    SpCarOrderEntity findByIdOrder(String IdOrder);

    /**
     * 任务有效订单
     * @oaram idTask
     * @return
     */
    @Query("select t from SpCarOrderEntity t where t.idTask =:idTask and t.status in(0,1,2,3,4,5,6,7)")
    List<SpCarOrderEntity> findSpOrderInService(@Param("idTask")String idTask);

    /**
     * 根据任务ID查找订单件数
     * @param idTask
     * @return
     */
    @Query("select count (t) from SpCarOrderEntity t where t.idTask=:idTask and t.status between 1 and 5")
    int findCountByIdTask(@Param("idTask")String idTask);

}
